package com.fangyu.lottery.controller;

import com.fangyu.lottery.exception.LotteryException;
import com.fangyu.lottery.model.LotteryResponse;
import com.fangyu.lottery.model.LotteryResult;
import com.fangyu.lottery.service.LotteryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author fangyu
 * @version v1.0.0
 * @since 2025/8/27 23:37
 */
@RestController
@RequestMapping("/api/lottery")
public class LotteryController {

    @Autowired
    private LotteryService lotteryService;

    @PostMapping("/draw")
    public ResponseEntity<LotteryResponse> drawPrize(
            @RequestParam Long activityId,
            @RequestParam Long userId) {

        try {
            LotteryResult result = lotteryService.draw(activityId, userId);
            return ResponseEntity.ok(LotteryResponse.success(result));
        } catch (LotteryException e) {
            return ResponseEntity.ok(LotteryResponse.fail(e.getMessage(), e.getCode()));
        }
    }

}